package zhaixf.fx.servlet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import zhaixf.fx.DAO.PassDAO;
import zhaixf.fx.DAO.RecordDAO;
import zhaixf.fx.DAO.ToolDAO;
import zhaixf.fx.DAO.TroubleDAO;
import zhaixf.fx.been.Pass;
import zhaixf.fx.been.Record;
import zhaixf.fx.been.Tool;
import zhaixf.fx.been.Trouble;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
//@RequestMapping("/fxapp")
public class HtmlController {
    @Autowired
    ToolDAO toolDAO;
    @Autowired
    PassDAO passDAO;
    @Autowired
    TroubleDAO troubleDAO;
    @Autowired
    RecordDAO recordDAO;

    @GetMapping("/toolHtml")
    public ModelAndView toolHtml(){return new ModelAndView("html/tool.html");}

    @GetMapping("/passHtml")
    public ModelAndView passHtml(){return new ModelAndView("html/pass.html");}

    @GetMapping("/troubleHtml")
    public ModelAndView troubleHtml(){return new ModelAndView("html/trouble.html");}

    @GetMapping("/recordHtml")
    public ModelAndView recordHtml(){return new ModelAndView("html/record.html");}

/////////////////////////////////////////////////////////////////////////////////////////////

    @PostMapping("/getAllTool")
    public List<Tool> getAllTool(){
        return toolDAO.queryAllTool();
    }

    @PostMapping("/addTool")
    public Map addTool(Tool tool){
        int i = toolDAO.addTool(tool);
        return resultString(i,"添加");
    }

    @PostMapping("/updateTool")
    public Map updateTool(Tool tool){
        int i = toolDAO.updateTool(tool);
        return resultString(i, "修改");
    }

    @PostMapping("/delTool")
    public Map delTool(String id){
        int i = toolDAO.delTool(id);
        return resultString(i, "删除");
    }

    @PostMapping("/getToolInfoById")
    public Map getToolInfoById(String id){
        Map map = new HashMap();
        Tool tool = toolDAO.queryById(id);
        List<Record> list = recordDAO.getRecordsByToolId(id);
        map.put("tool", tool);
        map.put("list", list);
        return map;
    }

/////////////////////////////////////////////////////////////////////////////////////////

    @PostMapping("/getAllPass")
    public List<Pass> getAllPass(){
        return passDAO.getAllPass();
    }

    @PostMapping("/addPass")
    public Map addPass(Pass pass){
        int i = passDAO.add(pass);
        return resultString(i,"添加");
    }

    @PostMapping("/updatePass")
    public Map updatePass(Pass pass){
        int i = passDAO.update(pass);
        return resultString(i, "修改");
    }

    @PostMapping("/delPass")
    public Map delPass(String name){
        int i = passDAO.del(name);
        return resultString(i, "删除");
    }

///////////////////////////////////////////////////////////////////////////////////////

    @PostMapping("/getAllTrouble")
    public List<Trouble> getAllTrouble(){
        return troubleDAO.getAllTrouble();
    }

    @PostMapping("/addTrouble")
    public Map addTrouble(Trouble trouble){
        int i = troubleDAO.add(trouble);
        return resultString(i, "添加");
    }

    @PostMapping("/delTrouble")
    public Map delTrouble(Trouble trouble){
        int i = troubleDAO.del(trouble);
        return resultString(i, "删除");
    }


    private Map resultString(int i, String tag){
        Map map = new HashMap();
        if (i==1){
            map.put("re", true);
            map.put("msg", tag+"成功");
        }else {
            map.put("re", false);
            map.put("msg", tag+"失败");
        }
        return map;
    }
}
